package LeetcodeTest_thirty_six;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class reconstructQueue
{
    public static void main(String[] args)
    {
        int[][] people={{7,0},{4,4},{7,1},{5,0},{6,1},{5,2}};
        int[][] answer;
        answer=Solutions.reconstructQueue(people);
        for(int i=0;i<answer.length;i++)
        {
            for(int j=0;j<answer[0].length;j++)
            {
                System.out.print(answer[i][j]+"  ");
            }
            System.out.println();
        }
    }
}
class Solutions
{
    public static int[][] reconstructQueue(int[][] people)
    {
        int m,n;
        m=people.length;
        n=people[0].length;
        int[][] result=new int[m][n];
        Comparator<int[]> comparator=new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2)
            {
                if(o1[0]==o2[0] && o1[1]>o2[1])
                    return 1;
                else if(o1[0]==o2[0] && o1[1]<o2[1])
                    return -1;
                else if(o1[0]<o2[0])
                    return 1;
                else
                    return -1;
            }
        };
        Arrays.sort(people,comparator);
        List<int[]> answer=new ArrayList<int[]>();
        for(int[] value:people)
        {
            answer.add(value[1],value);
        }
        for(int i=0;i<result.length;i++)
        {
            result[i]=answer.get(i);
        }
        return result;
    }

}

